#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void rep1();
void floatPoint1(float a, float b, float c, float d);
void mmx1();
void printBin(int value,char numchar);

void showRegistries()
//print to screen registries status
{
	int e;
	short int x;
	char h,l;
	char* bin;
	printf("o-----------------------------------o\n");
	//print EAX Hex
	__asm
	{
		MOV e,EAX
	    MOV x,AX
		MOV h,AH
		MOV l,AL
	}
	printf("|%35X| EAX Hex\n",e);
	//print EAX dec
	printf("|% 35d| EAX Dex\n",e);
	printf("|   ");
	printBin(e,32);
	printf("| EAX Bin\n");	
	printf("|-----------------|-----------------|\n");
	//print AX Hex
	printf("|                 |% 17X| AX Hex\n",x);
	//print AX dec
	printf("|                 |% 17d| AX Dec\n",x);
	printf("|                 | ");
	printBin(x,16);
	printf("| AX Bin\n");
	printf("|-----------------|--------|--------|\n");
	//print AH|AL dec
    printf("|                 |% 8X",h);
	printf("|% 8X| AH|AL Hex\n",l);
	printf("|                 |% 8d",h);
	printf("|% 8d| AH|AL Dec\n",l);
	printf("|                 |");
	printBin(h,8);
	printf("|");
	printBin(l,8);
	printf("| AH|AL Bin\n");
	printf("o-----------------------------------o\n");
}

void printBin(int value,char numchar) {
	char i;
	int x;
	char r;
	char* bin;
    bin=(char*)malloc(sizeof(char)*(1+numchar));
	x=value;
	for (i=0;i<numchar;i++) {
	  r=x%2;
	  bin[numchar-i-1]=48+r;
	  x=(x-r)/2;
	}
	bin[numchar]=0;
	printf("%s",bin);
}/*tobin*/

void main() {
    unsigned int a=2;
	unsigned int b=556;
	unsigned somma;

	__asm
	{
	    MOV EAX,a
	    ADD EAX,b
		MOV somma,EAX
	}

	printf("%d",somma);
	system("pause");
	/*
	rep1();
	system("pause");
	floatPoint1(1.2,3.4,5,0.12);
	system("pause");
	mmx1();
	system("pause");
	*/
}

void rep1()
//ciclo assegnazione con REP
{
	char x[17];
	strcpy(x,"1234567890abcdef\0");
	printf("%s\n",x);
	__asm
	{
		CLD
		MOV ECX,16
		LEA EDI,x
		XOR AL, AL
		MOV AL,45
		REP STOS
	}
	printf("%s\n",x);

}

void floatPoint1(float a, float b, float c, float d)
// ( a + b ) * ( c - d )
{
	float res;
		
	//Operazioni con Floating Point Unit
	printf("( %f + %f ) * ( %f - %f )\n",a,b,c,d);
	__asm
	{
		FINIT
		FLD a
		FADD b
		FLD c
		FSUB d
		FMUL
		FST res
	}
	printf("( %f + %f ) * ( %f - %f ) = %f \n",a,b,c,d,res);
} //Fine Operazioni con Floating Point Unit

void mmx1()
//Operazioni MMX
{
	unsigned char Vettore1[]={10,20,30,40,50,60,70,80};
	unsigned char Vettore2[]={80,70,60,50,40,30,20,10};
	unsigned char Vettore3[8];

	//Operazioni MMX
	__asm
	{
		EMMS
	    MOVQ MM0,Vettore1
		MOVQ MM1,Vettore2
		PADDB MM0,MM1
		MOVQ Vettore3,MM0
	}
	printf("%d|",Vettore3[0]);
	printf("%d|",Vettore3[1]);
	printf("%d|",Vettore3[2]);
	printf("%d|",Vettore3[3]);
	printf("%d|",Vettore3[4]);
	printf("%d|",Vettore3[5]);
	printf("%d|",Vettore3[6]);
	printf("%d",Vettore3[7]);
}  //Fine Operazioni MMX
